package com.hengtiansoft.lfy.chart.utils;

import android.content.Context;
import android.text.TextUtils;
import android.util.Log;
import android.widget.ImageView;
import android.widget.TextView;

import com.android.volley.Request.Method;
import com.android.volley.Response.ErrorListener;
import com.android.volley.Response.Listener;
import com.android.volley.VolleyError;
import com.hengtiansoft.lfy.chart.applib.controller.HXSDKHelper;
import com.google.gson.reflect.TypeToken;
import com.hengtiansoft.lfy.chart.DemoHXSDKHelper;
import com.hengtiansoft.lfy.LFYApplication;
import com.hengtiansoft.lfy.R;
import com.hengtiansoft.lfy.chart.action.GetUserJson;
import com.hengtiansoft.lfy.chart.bean.BaseResult;
import com.hengtiansoft.lfy.chart.bean.getUserByUsernameBean;
import com.hengtiansoft.lfy.constants.Constants;
import com.hengtiansoft.lfy.chart.db.UserDao;
import com.hengtiansoft.lfy.chart.domain.FriendsUser;
import com.hengtiansoft.lfy.chart.net.JsonListRequest;
import com.hengtiansoft.lfy.chart.net.VolleyUtil;
import com.hengtiansoft.lfy.utils.Config;
import com.hengtiansoft.lfy.chart.applib.utils.HXPreferenceUtils;
import com.squareup.picasso.Picasso;

import java.io.IOException;
import java.lang.reflect.Type;

public class UserUtils {

    /**
     * 根据username获取相应user，由于demo没有真实的用户数据，这里给的模拟的数据；
     * 
     * @param username
     * @return
     */
    public static FriendsUser getUserInfo(String username) {
        FriendsUser user = ((DemoHXSDKHelper) HXSDKHelper.getInstance())
                .getContactList().get(username);
        if (user == null) {
            user = new FriendsUser(username);
        }
        if (user != null) {
            // demo没有这些数据，临时填充
            if (TextUtils.isEmpty(user.getNick()))
                user.setNickname(username);
        }
        return user;
    }

    /**
     * 设置群头像
     */
    public static void setCurrentGroupAvatar(Context context,
                                             ImageView imageView, String groupid) {
        UserDao userDao = new UserDao(context);
        if (userDao.getGroupAvatar(groupid) != null) {
            Picasso.with(context)
                    .load(Constants.GETIMAGE + userDao.getGroupAvatar(groupid))
                    .placeholder(R.drawable.default_group_avatar)
                    .into(imageView);
        } else {
            Picasso.with(context).load(R.drawable.default_group_avatar)
                    .into(imageView);
        }
    }

    /**
     * 设置头像(需拼接)
     */
    public static void setCurrentImagePath(Context context, ImageView imageView,
                                           String imagePath) {
        if (imagePath != null) {
            Picasso.with(context).load(Constants.GETIMAGE + imagePath)
                    .placeholder(R.drawable.default_avatar).into(imageView);
        } else {
            Picasso.with(context).load(R.drawable.default_avatar)
                    .into(imageView);
        }
    }

    /**
     * 设置头像(无需拼接)
     */
    public static void setCurrentFullImagePath(Context context,
                                               ImageView imageView, String imagePath) {
        if (imagePath != null) {
            Picasso.with(context).load(imagePath)
                    .placeholder(R.drawable.default_avatar).into(imageView);
        } else {
            Picasso.with(context).load(R.drawable.default_avatar)
                    .into(imageView);
        }
    }

    /**
     * 设置用户头像
     * 
     * @param username
     */
    public static void setUserAvatar(Context context, String username,
                                     ImageView imageView) {
        FriendsUser user = getUserInfo(username);
        if (user != null && user.getAvatar() != null) {
            Picasso.with(context).load(user.getAvatar())
                    .placeholder(R.drawable.default_avatar).into(imageView);
        } else {
            Picasso.with(context).load(R.drawable.default_avatar)
                    .into(imageView);
        }
    }

    /**
     * 设置当前用户头像
     */
    public static void setCurrentUserAvatar(Context context,
            ImageView imageView) {
        if (HXPreferenceUtils.getInstance().getCurrentUserAvatar() != null) {
            Picasso.with(context)
                    .load(HXPreferenceUtils.getInstance()
                            .getCurrentUserAvatar())
                    .placeholder(R.drawable.default_avatar).into(imageView);
        } else {
            Picasso.with(context).load(R.drawable.default_avatar)
                    .into(imageView);
        }
    }

    /**
     * 设置当前背景
     * 
     * @throws IOException
     */
    public static void setCurrentUserBackground(Context context,
            ImageView imageView) {
        if (HXPreferenceUtils.getInstance()
                .getCurrentUserBackground() != null) {
            Picasso.with(context)
                    .load(HXPreferenceUtils.getInstance()
                            .getCurrentUserBackground())
                    .placeholder(R.drawable.me_img).into(imageView);
        } else {
            Picasso.with(context).load(R.drawable.me_img).into(imageView);
        }
    }

    /**
     * 设置用户昵称
     */
    public static void setUserNick(String username, TextView textView) {
        FriendsUser user = getUserInfo(username);
        if (user != null) {
            textView.setText(user.getNick());
        } else {
            textView.setText(username);
        }
    }

    /**
     * 设置当前用户昵称
     */
    public static void setCurrentUserNick(TextView textView) {
        FriendsUser user = ((DemoHXSDKHelper) HXSDKHelper.getInstance())
                .getUserProfileManager().getCurrentUserInfo();
        if (textView != null) {
            textView.setText(user.getNick());
        }
    }

    /**
     * 保存或更新某个用户
     * 
     * @param newUser
     */
    public static void saveUserInfo(FriendsUser newUser) {
        if (newUser == null || newUser.getUsername() == null) {
            return;
        }
        ((DemoHXSDKHelper) HXSDKHelper.getInstance()).saveContact(newUser);
    }

    /**
     * 从服务器获取登录者的相关信息
     * 
     * @param context
     * @param username
     * @param imageView
     */
    public static void getUserByUsernameRequest(final Context context,
                                                String username, final ImageView imageView) {
        GetUserJson getUserJson = new GetUserJson(
                Config.getUserToken(), username);
        Type type = new TypeToken<BaseResult<getUserByUsernameBean>>() {
        }.getType();
        JsonListRequest<BaseResult<getUserByUsernameBean>> request = new JsonListRequest<BaseResult<getUserByUsernameBean>>(
                context, Method.POST, Constants.getUserByUsername,
                getUserJson.toString(), null, null, type, false,
                new Listener<BaseResult<getUserByUsernameBean>>() {

                    @Override
                    public void onResponse(
                            BaseResult<getUserByUsernameBean> arg0) {
                        int code = 1;
                        getUserByUsernameBean data = null;
                        code = arg0.getCode();
                        String imagePath = null;
                        if (code == 0) {
                            data = arg0.getData();
                            if (data != null) {
                                imagePath = data.getImagepath();
                                if (imagePath != null) {
                                    Log.i("imagePath", imagePath);
                                    Picasso.with(context)
                                            .load(Constants.GETIMAGE + imagePath)
                                            .placeholder(
                                                    R.drawable.default_avatar)
                                            .into(imageView);
                                } else {
                                    Picasso.with(context)
                                            .load(R.drawable.default_avatar)
                                            .into(imageView);
                                }
                            }
                        } else {
                        }
                    }
                }, new ErrorListener() {
                    @Override
                    public void onErrorResponse(VolleyError arg0) {
                    }
                });
        VolleyUtil.addRequest(request);
    }

}
